package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// TaskRecord 任务执行记录
// 任务执行完成后，上位机会上报任务信息到服务器保存
type TaskRecord struct {
	ID           string `json:"id"  gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	TaskID       string `json:"task_id" gorm:"column:task_id;type:string;size:40;comment:任务id"`
	RobotID      string `json:"robot_id" gorm:"column:robot_id;type:string;size:40;index:idx_robotID_createdDate,priority:1;index:idx_beginTime_robotID,priority:2;comment:机器人id"`
	TaskName     string `json:"task_name" gorm:"column:task_name;type:string;size:40;comment:任务名称"`
	TaskType     int    `json:"task_type" gorm:"column:task_type;type:int;comment:任务类型"`
	WorkType     int    `json:"work_type" gorm:"column:work_type;type:int;size:8;comment:工作类型,1-运营 2-回充 3-出门"`
	BeginTime    int64  `json:"begin_time" gorm:"column:begin_time;type:int;index:idx_beginTime_robotID,priority:1;comment:开始时间"`
	EndTime      int64  `json:"end_time" gorm:"column:end_time;type:int;comment:结束时间"`
	Odometer     int64  `json:"odometer" gorm:"column:odometer;type:int;comment:任务里程"`
	MapName      string `json:"map_name" gorm:"column:map_name;type:string;size:40;commnet:地图名称"`
	CurrentIndex int64  `json:"current_index" gorm:"column:current_index;type:int;size:64;comment:当前次数"`
	TotalIndex   int64  `json:"total_index" gorm:"column:total_index;type:int;size:64;comment:总次数"`
	TimeDuration int64  `json:"time_duration" gorm:"column:time_duration;type:int;size:64;comment:任务执行时长,单位ms"`
	CreatedMonth string `json:"created_month,omitempty" gorm:"column:created_month;type:string;size:10;index:created_month;comment:上报月份"`
	CreatedDate  string `json:"created_date,omitempty" gorm:"column:created_date;type:string;size:10;index:idx_robotID_createdDate,priority:2;comment:上报日期"`
	CreatedAt    int64  `json:"created_at" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	User         string `json:"user" gorm:"column:user;type:string;size:40;comment:操作人"`
}

// TableName 数据库表名
func (TaskRecord) TableName() string {
	return "task_record"
}

// BeforeCreate 自增id
func (t *TaskRecord) BeforeCreate(tx *gorm.DB) (err error) {
	if t.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	t.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &TaskRecord{})
}
